package BinaryTreeExer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-07-20
 * Time: 16:58
 */

/**
 * 对称二叉树:
 *
 * 给你一个二叉树的根节点 root ， 检查它是否轴对称。
 *
 *
 *
 * 示例 1：
 *
 *
 * 输入：root = [1,2,2,3,4,4,3]
 * 输出：true
 * 示例 2：
 *
 *
 * 输入：root = [1,2,2,null,3,null,3]
 * 输出：false
 */

public class BinaryTreeExercise4 {

    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, BinaryTreeExercise2.TreeNode left, BinaryTreeExercise2.TreeNode right) {
            this.val = val;
        }
    }

    public boolean isSymmetric(TreeNode root) {
        if(root == null) {
            return true;
        }

        return isSymmetric(root.left, root.right);
    }
    //封装起来判断，每个树的左右子树是否对称
    public boolean isSymmetric(TreeNode leftTree, TreeNode rightTree) {
        //结构
        if(leftTree == null && rightTree != null ||
                leftTree != null && rightTree == null) {
            return false;
        }

        if(leftTree == null && rightTree == null) {
            return true;
        }

        //值
        if(leftTree.val != rightTree.val) {
            return false;
        }

        //开始判断对称,现在leftTree和rightTree都作为根
        return isSymmetric(leftTree.left,rightTree.right) &&
                isSymmetric(leftTree.right,rightTree.left);

    }

}
